###################################################################
# THIS FILE CREATES THE GRAPHS USED IN THE PUBLICATION VERSION  OF#
# WHERE'S THE BIAS? A REASSESMENT OF THE CHILEAN ELECTORAL SYSTEM #
#                                                                 #
###################################################################
library(xtable)
library(foreign)
setwd("~/Dropbox/Data/Paper-Chilean Electoral System/Temp")
#chile1<-read.dta("chile1.dta")
chile<-read.csv("chile1.csv")
attach(chile)
old.par <- par(mfrow=c(1,1),ask=F)

#GRAPH 1
chile.reg<-lm(ss ~ vs, subset(chile, filter2 == 0 & house == 1))
allc.camara   <-subset(chile, filter2 == 0 & house == 1)

plot(allc.camara$vs,allc.camara$ss
    #, main="Seat and Vote Share by Coalition (Camara)"
    , xlab= "Vote Share"
    , ylab="Seat Share"
    , pch = ifelse(allc.camara$filter1==1, 17, ifelse(allc.camara$filter1==2, 3, 20))
    , col = "blue"
    , ylim=range(0,60), xlim=range(0,60)
    )
    legend(60, 0, c("Concertacion", "Alianza", "Others")
        , col = "blue"
        , pch = c(17, 3, 20)
        , xjust = 1
        , yjust = 0
        , bty = "n" ) 
    abline(0,1, lty=1)              #adds 45 degree line                          
    abline(chile.reg$coefficients[1],chile.reg$coefficients[2]
        , lty=2)                #adds the regression line to the graph



#DISTORTION GRAPHS
par(mfrow=c(1,1))#,ask=FALSE)
plot(allc.camara$vs, allc.camara$dif
      , ylab= "Absolute Distortion"
    , xlab="Vote Share"
    , pch = ifelse(allc.camara$filter1==1, 17
        , ifelse(allc.camara$filter1==2, 3, 20))  #selects the symbol
    , col = "blue"
    , ylim=c(-8,8), xlim=c(0,60)
    , cex.axis = 0.8
    )
    legend(60, -8, c("Concertacion", "Alianza", "Others")
        , col = "blue"
        , pch = c(17, 3, 20)
        , xjust = 1
        , yjust = 0
        , cex = 0.8) 
    abline(h=0)                                         #v= value for a vertical line
#savePlot(filename="absdistortion", type=c("ps"))

plot(allc.camara$vs, allc.camara$dif2
    #, main= "Relative Distortion"
    , ylab= "Relative Distortion"
    , xlab= "Vote Share"
    , pch = ifelse(allc.camara$filter1==1, 17, ifelse(allc.camara$filter1==2, 3, 20))
    , col = "blue"
    , ylim= range(-150,150), xlim=range(0,60)
    , cex.axis = 0.8
    )
    legend(60, -150, c("Concertacion", "Alianza", "Others")
        , col = "blue"
        , pch = c(17, 3, 20)
        , xjust = 1
        , yjust = 0
        , cex = 0.8
        ) 
    abline(h=0)                           #v= value for a vertical line
#savePlot(filename="reldistortion", type=c("ps"))
rm(chile, allc.camara)


#APPORTIONMNET GRAPH
chile<-read.dta("chile2.dta") 
house <- subset(chile, house == 1)
par(mfrow=c(1,1),mar=c(3.5,3.5,2,1),ask=FALSE)
plot(house$tvot,house$ccts/100,
    , pch= ifelse(house$ccte==0, 3, ifelse(house$ccte==1, 20, 17))
    , ylim= range(.10,.85)
    , ylab= " "
    , xlab= " "
    , col = "blue"
    , cex.axis = 1
    , main = "Concertacion")
    legend(max(house$tvot), .10
        , c("0 Seats Won", "1 Seats Won", "2 Seats Won")
        , pch = c(3, 20, 17)
        , xjust = 1
        , yjust = 0
        , cex = 1
        , col = "blue"
        ) 
    cctsize.reg <- lm (house$ccts/100 ~ house$tvot) 
    abline(cctsize.reg$coefficients[1],cctsize.reg$coefficients[2]
        , lty=2)           
    mtext("Valid Votes Cast", side = 1, line = 2)
    mtext("Concertacion's Vote Share", side = 2, line = 2)

plot(house$tvot,house$ders/100,
    , pch= ifelse(house$dere==0, 3, ifelse(house$dere==1, 20, 17)),
    , ylim= range(.1,.85)
    , ylab= " "
    , xlab= " "
    , col = "blue"
    , cex.axis = 1
    , main = "Alianza")
    legend(min(house$tvot), 0.85
        , c("0 Seats Won", "1 Seats Won", "2 Seats Won")
        , pch = c(3, 20, 17)
        , xjust = 0
        , yjust = 1
        , cex = 1
        , col = "blue"
        )
    dersize.reg <- lm (house$ders/100 ~ house$tvot) 
    abline(dersize.reg$coefficients[1],dersize.reg$coefficients[2]
        , lty=2)           
    mtext("Valid Votes Cast", side = 1, line = 2)
    mtext("Alianza's Vote Share", side = 2, line = 2)
rm(chile, i, dersize.reg, temp, mymodel.comp, cctsize.reg, chile.reg, ruler)


#Correlation between size of district and vote share
cor.der<-cor.test(house$tvot,house$ders/100, alternative = "two.sided",  method="pearson") 
cor.cct<-cor.test(house$tvot,house$ccts/100, alternative = "two.sided",  method="pearson") 
cat("\n","Correlation between size of district and vote share","\n",
    "Alianza:     ",cor.der$estimate,"with a p-value of",cor.der$p.value,"\n",
    "Concertacion:",cor.cct$estimate,"with a p-value of",cor.cct$p.value,"\n")

#HISTOGRAM #Concertacion 
par(mfrow=c(1,1),mar=c(4,4,1,1), ask=F)
hist(house$ccts
    , breaks=19
    , col=8
    , freq = FALSE
    , main = ""
    , xlim = c(0,100)
    , xlab = "Concertacion's Vote Share"
    , cex.axis = 0.8
    )
#Kernel Density Plot for Concertacion 
d <- density(house$ccts, kernel = c("gaussian"))  
lines(d$x,d$y)
#This adds the normal curve to the graph
my.norm <-rnorm(10000,mean=mean(house$ccts),sd=sd(house$ccts))
dnc <- density(my.norm, kernel = c("gaussian")) 
lines(dnc$x,dnc$y,lty=2)
                                #Alianza
hist(house$ders
    , breaks=19
    , col=8
    , freq = FALSE
    , main = ""
    , xlim = c(0,100)
    , xlab = "Alianza's Vote Share"
    , cex.axis = 0.9
    )
#Kernel Density Plot 
d <- density(house$ders, kernel = c("gaussian"))  
lines(d$x,d$y)
#This adds the normal curve to the graph
my.norm <-rnorm(10000,mean=mean(house$ders),sd=sd(house$ders))
dnd <- density(my.norm, kernel = c("gaussian")) 
lines(dnd$x,dnd$y,lty=2)
rm(my.norm, dnd, dnc)

#BOX PLOT GRAPH  
par(mfrow=c(1,1),mar=c(4,4,1,1),ask=F)
                    # This basically overlays two box-plots on top of each other
boxplot(house$ccts ~ house$year, ylim=range(0,100)
        , cex.axis=0.7
        , boxwex=0.4
        , at = 1:4 - 0.25
        , border = 1    #color of the border
        , xaxt = "n"    #omit x axis
        , ylab = ("Vote Share")
        )
boxplot(house$ders ~ house$year
        , ylim=range(0,100)
        , cex.axis=0.8
        , boxwex = 0.4
        , add = TRUE
        , col = "blue"  #color of the body
        , xaxt = "n"    #omit x axis
        , yaxt = "n"
        , at = 1:4 + 0.25)    
axis(1, at = 1:4, labels = c("1989", "1993", "1997", "2001"), tick = FALSE, cex.axis=0.7)         
legend(3.5, 100, c("Concertacion", "Alianza")
        , fill = c(0,"blue")
        , xjust = 0
        , yjust = 1
        , cex = 0.9
        ) 

